package aad.storyboard.model.menu;

import aad.storyboard.controller.ControllerManager;
import aad.storyboard.controller.DBAdapter;
import aad.storyboard.model.Chapter;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Story;
import android.database.Cursor;

/**
 * director class of the builder pattern
 */
public class MenuProvider {
	
	private Story story;
	private DBAdapter adapter;
	private static MenuProvider instance = new MenuProvider();
	
	public static MenuProvider getInstance(Story s){
		instance.setStory(s);
		instance.setAdapter(ControllerManager.getInstance().getAdapter());
		return instance;
	}


/**
 * builds the main menu
 */
	public void buildInitialMenu(MenuBuilder builder){
		builder.initialise();
		builder.createLookSettings();
		builder.createTitle("Main Menu");
		builder.createText("Welcome to the Jabberwocky land. Select what you want to do next!");
		builder.createStartButton();
		builder.createChapterButton();
		builder.createBookmarkSelectionButton();
		builder.createSettingsButton();
		builder.createExitButton();
	}
	/**
	 * builds the pause menu
	 */
	public void buildPauseMenu(MenuBuilder builder){
		builder.initialise();
		builder.createTitle("Pause");
		builder.createLookSettings();
		builder.createResumeButton();
		builder.createSaveBookMarkButton();
		builder.createSettingsButton();
		builder.createExitButton();
	}
	/**
	 * builds the final menu
	 */
	public void buildFinalMenu(MenuBuilder builder){
		builder.initialise();
		builder.createTitle("The End");
		builder.createLookSettings();
		builder.createExitButton();
		
	}
	/**
	 * builds the chapter selection menu
	 */
	public void buildChapterSelectionMenu(MenuBuilder builder){
		builder.initialise();
		builder.createLookSettings();
		builder.createTitle("Chapters");
		for(Chapter c : Story.getInstance().getChapters()){
			System.out.println("Chapter" +c.getId()+c.getStartTime());
			builder.createChapterSelectionButton(c.getId());
		}
		builder.createMainMenuButton();
		builder.createExitButton();
		
	}

	
	private void setStory(Story s) {
		this.story = s;
		
	}
	/**
	 * builds the scene selection menu
	 */
	public void buildSceneMenu(MenuBuilder builder,int i) {
		builder.initialise();
		builder.createLookSettings();
		builder.createTitle("Scenes");
		Chapter chapter = null;
		Scene scene = null;
		for(Chapter c : story.getChapters()){
			if(c.getId() == i){
				for(Scene s : c.getScenes()){
					builder.createSceneSelectionButton(s.getId());
				}
			}
		}
		builder.createMainMenuButton();
		builder.createExitButton();
		
	}
	/**
	 * builds the settings menu
	 */
	public void buildSettingsMenu(MenuBuilder builder){
		builder.initialise();
		builder.createLookSettings();
		builder.createTitle("Settings");
		builder.createVolumeController();
		builder.createWikiButton();
		builder.createScriptButton();
		builder.createMainMenuButton();
		builder.createExitButton();
		
	}
	/**
	 * builds the bookmarking menu
	 */
	public void buildBookmarkMenu(MenuBuilder builder) {
		builder.initialise();
		builder.createLookSettings();
		builder.createTitle("Bookmarks");
		Cursor dbCursor = adapter.fetchAllBookmarks();
		dbCursor.moveToFirst();
		int idIdx = dbCursor.getColumnIndex(DBAdapter.KEY_ROWID);
		int sceneIdx = dbCursor.getColumnIndex(DBAdapter.KEY_SCENE);
		int timeIdx = dbCursor.getColumnIndex(DBAdapter.KEY_TIME);
		int descIdx = dbCursor.getColumnIndex(DBAdapter.KEY_DESCRIPTION);
		if(dbCursor.getCount() == 0)
			builder.createText("Currently, there are no bookmarks stored on this device.");
		else{
			while(!dbCursor.isLast()){
				if(!dbCursor.isNull(idIdx)){
					String data = dbCursor.getString(sceneIdx);
					data += ", "+dbCursor.getString(timeIdx);
					
					if(!dbCursor.isNull(descIdx)){
						data += ", "+dbCursor.getString(descIdx);
					}
					System.out.println("FOUND ONE RECORD, NOW MAKING BUTTON");
					builder.createBookMarkButton(data);
					dbCursor.moveToNext();
				}
			}
		}
		builder.createMainMenuButton();
		builder.createExitButton();
		
	}

	private void setAdapter(DBAdapter adapter) {
		this.adapter = adapter;
		
	}
	

}
